package com.oopnet.template.service;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oopnet.template.dao.PersonnelMapper;
import com.oopnet.template.dao.StudentMapper;
import com.oopnet.template.dao.TeacherMapper;
import com.oopnet.template.entity.Personnel;
import com.oopnet.template.entity.Student;
import com.oopnet.template.entity.Teacher;

@Service
public class PersonnelService {
	
	@Autowired
	private PersonnelMapper mapper;
	private BasicDataSource dataSource;
	
	public BasicDataSource getDataSource() {
//		this.dataSource.
		return dataSource;
	}
	
	@Test
	public void testStudent() throws Exception {
		ApplicationContext ctxt = new ClassPathXmlApplicationContext("classpath:spring-applicationContext.xml");
		StudentMapper mapper = ctxt.getBean(StudentMapper.class);
		Student stu = mapper.selectStudentById(2);
		System.out.println("*********************");
		System.out.println(stu.getTeacher());
	}
	
	@Test
	public void testTeacher() throws Exception {
		@SuppressWarnings("resource")
		ApplicationContext ctxt = new ClassPathXmlApplicationContext("classpath:spring-applicationContext.xml");
		
		TeacherMapper tmapper = ctxt.getBean(TeacherMapper.class);
		StudentMapper mapper2 = ctxt.getBean(StudentMapper.class);
		Student stu = new  Student();
		stu.setName("Abley");
		stu.setTeacher_id(1);
		mapper2.insertStudent(stu);
		
//		Teacher tea = mapper.selectTeacherById(1);
//		System.out.println(tea);
	}
	public static void main(String[] args) {
		ApplicationContext ctxt = new ClassPathXmlApplicationContext("classpath:spring-applicationContext.xml");
		PersonnelMapper mapper = (PersonnelMapper)ctxt.getBean(PersonnelMapper.class);
//		PersonnelMapper mapper = (PersonnelMapper)ctxt.getBean(PersonnelMapper.class);
//		System.out.println(mapper.selectByPrimaryKey(10));
//		System.out.println(mapper.se);
		
//		System.out.println(mapper.selectAll(new RowBounds(1,2)));
		
		PageHelper.startPage(2, 2);
		List<Personnel> personnels = mapper.selectAllByPageHelper() ;
		System.out.println(personnels.size());
		for (Personnel personnel : personnels) {
			System.out.println(personnel.getCnName());
		}
		
		PageInfo<Personnel> pageInfo = new PageInfo<Personnel>(personnels);
		long total = pageInfo.getTotal();
		System.out.println(total);
		
		Personnel personnel = new Personnel();
//		personnel.setId(3);
		personnel.setCnName("小姚");
		personnel.setCompanyId("12");
		personnel.setEmployeeId("1016977");
		personnel.setDepartment("CDC");
		personnel.setSuperiorName("Gary");
		int i =mapper.insert(personnel);
		System.out.println( "Insert : "+i);
//		ObjectFactory factory = new DefaultObjectFactory();
//		@SuppressWarnings("unchecked")
//		List<String> list = factory.create(ArrayList.class);
//		list.add("A");
//		System.out.println(list);
	}
}
